import des from "./des";
import env from "./env";

function request(
  url,
  data = {},
  method = "GET",
  needSecret,
  header = "application/json"
) {
  return new Promise(function (resolve, reject) {
    // 处理权限的页面
    // if (!data.needToken) {
    //   if (!wx.getStorageSync("token")) {
    //     wx.switchTab({
    //       url: "/pages/personalCenter/personalCenter",
    //     });
    //     resolve({})
    //     return
    //   }
    // }
    let dataInfos = "";
    if (!needSecret) {
      dataInfos = JSON.stringify(data);
    } else {
      dataInfos = data;
    }
    wx.request({
      url: env.baseUrl + url,
      data: needSecret ?
        dataInfos : des.encrypt(dataInfos, "98fbffd1064a4355b8abaacb6fa96f94"),
      method: method,
      header: {
        "Content-Type": header,
        token: wx.getStorageSync("token") || "",
      },
      success: (res) => {
        // console.log(res, "接口返回的结果");
        if (res.statusCode == 200) {
          if (needSecret) {
            resolve(res.data);
          } else {
            var data = des.decrypt(
              res.data.data,
              "98fbffd1064a4355b8abaacb6fa96f94"
            );
            // 判断账号是否唯一
            // console.log(JSON.parse(data), "JSON.parse(data)");
            if (JSON.parse(data).code == 400) {
              // wx.setStorageSync("token", "cxgc123456");
              wx.removeStorageSync("userInfo");
              // wx.navigateTo({
              //   url: "/pages/launch_start/launch_start",
              // });
            }
            resolve(JSON.parse(data));
          }
        } else {
          resolve(res)
        }
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
}

/**
 *  notVipBindFI	非VIP绑定金融机构用户
    vipBindFI	VIP绑定金融机构用户
    svipBindFI	SVIP绑定金融机构用户
    bindGOV	绑定政府机构用户
    administrator	超级管理员
    cbrc 银保监用户
    ordinary	普通用户
    visitor 游客
 */
function checkVipStatus(data = 'Y') {
  const parmas = {
    needCount: data
  }
  console.log(parmas, '是否计算')
  return new Promise((resolve, reject) => {
    request(
      // "/user/query_follow_corps",
      "/user/getTrialUserUsageCount", parmas,
      "POST"
    ).then(res => {
      console.log(res, '角色等级')
      const info = wx.getStorageSync('userInfo')
      if (info && info.phone === '18852956682') {
        resolve('administrator')
      } else {
        if (res.data && res.data.memberId) {
          resolve(res.data.memberId)
        } else {
          resolve('visitor')
        }
      }
    }).catch(res => {
      resolve('visitor')
    });
  })
}

function request_cydn(
  url,
  data = {},
  method = "GET",
  needSecret,
  header = "application/json"
) {
  return new Promise(function (resolve, reject) {
    let dataInfos = "";
    if (!needSecret) {
      dataInfos = JSON.stringify(data);
    } else {
      dataInfos = data;
    }
    // console.log(des.encrypt_cydn(dataInfos, "98fbffd1064a4355b8abaacb6fa96f94"), '000000000000');
    wx.request({
      url: env.baseUrl_cydn + url,
      data: needSecret ?
        dataInfos : des.encrypt_cydn(dataInfos, "98fbffd1064a4355b8abaacb6fa96f94"),
      method: method,
      header: {
        "Content-Type": header,
        "requestType": "post",
        Authorization: 'Bearer ' + wx.getStorageSync("token_cydn") || "",
      },
      success: (res) => {
        // console.log(res, "接口返回的结果");
        if (res.statusCode == 200) {
          if (needSecret) {
            resolve(res.data);
          } else {
            var data = des.decrypt(
              res.data.data,
              "98fbffd1064a4355b8abaacb6fa96f94"
            );
            if (JSON.parse(data).code == 400) {
              // resolve(JSON.parse(data));
              // wx.navigateTo({
              //   url: 'pages/launch_start/launch_start',
              // })
            } else {
              resolve(JSON.parse(data));
            }
          }
        } else {
          // reject(res.errMsg);
          resolve('');
        }
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
}

module.exports = {
  request,
  request_cydn,
  checkVipStatus
};